package com.ssb.admin.modules.sys.permission.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ssb.admin.common.controller.AbstractController;
import com.ssb.admin.common.utils.R;
import com.ssb.admin.common.utils.TreeNode;
import com.ssb.admin.modules.sys.permission.entity.SysPermission;
import com.ssb.admin.modules.sys.permission.service.ISysPermissionService;
import com.ssb.admin.modules.sys.role.entity.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@RestController
@Api(value = "模块管理", tags = "模块管理")
public class SysPermissionController extends AbstractController {
    private final ISysPermissionService sysPermissionService;
    @Autowired
    public SysPermissionController(ISysPermissionService sysPermissionService){
        this.sysPermissionService = sysPermissionService;
    }

    @PostMapping("/module/{type}/list")
    @ApiOperation(value = "获取{type}列表", notes = "用于获取菜单/按钮列表")
    @ApiImplicitParam(name="type", value="menu/button", required = true, dataType = "String")
    public R getTypeList(@PathVariable String type){
        Wrapper<SysPermission> ew =  new EntityWrapper<SysPermission>().eq("resource_type", type);
        List<SysPermission> sysPermissions = sysPermissionService.selectList(ew);
        return R.ok().list(1, sysPermissions);
    }
    @PostMapping("/module/list")
    @ApiOperation(value = "获取权限列表", notes = "用于获取所有菜单/按钮树形列表")
    public R getList(){
        List<TreeNode> sysPermissions = sysPermissionService.queryAllMenus();
        return R.ok().list(1, sysPermissions);
    }
    @PostMapping("/module/create")
    @ApiImplicitParam(name="sysPermission", value="权限对象实例", required = true, dataType = "SysPermission")
    @ApiOperation(value = "创建权限", notes = "创建权限")
    public R create( @RequestBody SysPermission sysPermission){
        sysPermissionService.save(sysPermission);
        return R.ok();
    }
    @PostMapping("/module/update")
    @ApiImplicitParam(name="sysPermission", value="权限对象实例", required = true, dataType = "SysPermission")
    @ApiOperation(value = "更新权限", notes = "更新权限")
    public R update(@RequestBody SysPermission sysPermission){
        sysPermissionService.save(sysPermission);
        return R.ok();
    }


}

